import javax.xml.bind.JAXBException;

/**
 * 
 * @author ryannikolaidis
 *
 * This is where performance events are scheduled.
 *
 */

public class Score implements Runnable {
	public int lastMotif = 0;
	public boolean following = false;
	public int malletLoc0 = 55;
	public int malletLoc1 = 95;
	public boolean mallet0 = true;
	public boolean mallet1= true;
	public int playing0=0;
	public int playing1=0;
	public int shimonPlaying0=0;
	public int shimonPlaying1=0;
	public boolean missedNote=false;
	public Object[] oscArray = new Object[2];
	public Object[] noteArray = new Object[3];
	public int[] intArray = new int[4];
	public int root = 0;
	public int arm = 0;
	public int module = 0;
	public boolean endrepeat = false;
	public int[][] scheduler = new int[2050][4]; 
		// 0 row = on or off (1 or 0)  
		// 1 row = identifier
		// 2 row = modulo 
		// 3 row = offset
		// >3 = extras
	public int[][] noteScheduler = new int[5][500]; 
		// 0 row = on or off (1 or 0)  
		// 1 row = time 
		// 2 row = note
		// 3 row = velocity
		// 4 row = arm
	public int count;
	public int[] pitchArray;
	public int[] velArray;
	public int[] durArray;
	public int[] armArray;
	public boolean metroOn=true;
	public boolean improvise=false;
	
	public void run() 
	{		
		metro();
//		Main.listenForBeat=false;
//		headbang(true);
		score1();

	}
	
	public void score1()
	{

	
	}
	
	public void test(int arm)
	{
		for (int i=0;i<4;i++)
		{
			noteOut(60+5*i,127,arm);
			waitMil(170.);
		}
		waitMil(500.);
	}

	public void waitTurnTaking(int before, int after)
	{
		listenPose();
		wait((float)before);
		waitResponse();
		wait((float)after);
	}
	
	public void waitNewSection()
	{
		wait(5f);
		waitResponse();
//		wait(2f);
		metroOn=false;
		clearScheduler();
		Main.listenForBeat=true;
		waitResponse();
		wait(.1);
		metroOn=true;
		metro();
		Main.listenForBeat=false;
	}

	
	public void melody1(int note,int modulo,int subdivision,int arm,int eventIndex)
	{

//		9 beats *highest note = note+10, *lowest note= note-2
		durArray=new int[] {100,50,50,100,50,50,100,50,50,100,100,50,50,100,50,50,50,50,100,50,50,100,50,50};
		pitchArray=new int[] {note,note+4,note+5,note+7,note+5,note+4,note+5,note+7,note+10,note+7,note+5,note,note-2,note,note+2,note+5,note+4,note+5,note+7,note+9,note+12,note+14,note+12,note+9};
		velArray=new int[pitchArray.length];
		noteOut(95,0,3);
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=70;
		}

		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm+1;
		}
		armArray[0]=arm;
		armArray[11]=arm;
		armArray[12]=arm;
		armArray[13]=arm;
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody2(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		20 beats arm+1 starts at note+4, *highest note = note+12 *lowest note=note-5
		armArray=new int[] {arm,arm,arm+1,arm,arm+1,arm,arm+1,arm,arm+1,arm+1,arm+1,arm+1,arm+1,arm+1,arm+1,arm+1,arm,arm,arm,arm,arm,arm+1,arm,arm,arm,arm,arm,arm+1};
		durArray=new int[] {100,50,100,50,100,50,100,50,50,50,100,100,50,100,50,100,50,50,50,50,50,50,100,100,50,100,50,100};
		pitchArray=new int[] {note,note,note+4,note,note+5,note,note+7,note,note+10,note+12,note+10,note+7,note+5,note+4,note+5,note+4,note,note-2,note-5,note-2,note,note+4,note,note,note-5,note-2,note,note+4};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=70;
		}
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	public void melody3(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		7 beats
		durArray=new int[] {100,50,50,50,50,100,50,50,100,50,50};
		pitchArray=new int[] {note,note+2,note+5,note+4,note+5,note+7,note+9,note+12,note+14,note+12,note+9};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=100;
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
		
			armArray[i]=arm+1;
		}
		armArray[0]=arm;
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody4(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		21 beats
		int vel = 65;
		armArray=new int[] {arm+1,	arm,	arm+1,	arm,	arm,	arm,	arm+1,	arm+1,	arm+1,	arm+1,	arm,	arm+1,		arm,	arm+1,		arm,	arm+1,		arm,	arm,	arm+1,	arm+1,	arm+1,	arm+1,	arm+1,	arm,	arm+1,		arm,	arm+1,		arm,	arm+1,		arm,		arm+1,	arm+1,	arm+1,	arm+1,	arm+1};
		durArray=new int[] {50,		50,		50,		50,		50,		50,		50,		50,		50,		50,		50,		100,		50,		100,		50,		100,		50,		50,		50,		50,		50,		50,		50,		50,		100,		50,		100,		50,		100,		50,			50,		50,		100,	50,		50};
		pitchArray=new int[] {note,	note-4,	note,	note-4,	note-2,	note-4,	note+1,	note+3,	note+5,	note+1,	note-4,	note+3,		note-4,	note+1,		note-7,	note+1,		note-4,note+-2,	note+3,	note+1,	note+3,	note+5,	note+1,	note-4,	note+10,	note-4,	note+8,		note-4,	note+5,		note-4,		note+3,	note+5,	note+1,	note+5,	note+1};
		velArray=new int[] {vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,		vel,	vel,		vel,	vel,		vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,	vel,		vel,	vel,		vel,	vel,		vel,		vel,	vel,	vel,	vel,	vel};
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody5(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		20 beats
		int vel = 105;
		armArray=new int[] {arm,	arm+1,		arm,		arm+1,		arm,		arm+1,		arm,		arm,		arm+1,		arm+1,		arm+1,		arm,		arm,		arm+1,		arm,		arm,		arm,		arm,		arm,		arm,		arm+1,		arm,		arm,		arm,		arm+1,		arm+1};
		durArray=new int[] {50,		50,			100,		0,			50,			0,			100,		50,			50,			50,			100,		50,			50,			50,			50,			50,			50,			100,		50,			50,			50,			50,			100,		150,		100,		50};
		pitchArray=new int[] {note,	note+5,		note-4,		note+5,		note-4,		note+5,		note-4,		note-4,		note+5,		note+7,		note+3,		note-4,		note,		note+3,		note,		note-2,		note-4,		note-4,		note-2,		note,		note+3,		note-4,		note-2,		note-4,		note+3,		note+3};
		velArray=new int[] {vel,	vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel};

		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	public void melody6(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
		// num beats 8
		// start 64
		// arm 2
		int vel = 127;
		armArray=new int[] {arm,		arm+1,		arm,		arm,		arm,		arm,		arm,		arm,		arm+1,		arm+1,		arm+1,		arm+1,		arm+1,		arm};
		durArray=new int[] 	{ 50,		50,			50,			50,			50,			50,			100,		50,			50,			50,			100,		50,			50,			50};
		pitchArray=new int[] {note, 	note+3, 	note-3, 	note-4, 	note-5, 	note-3, 	note-4, 	note-3, 	note, 		note+3, 	note+6, 	note+4, 	note+3, 	note+1};
		velArray=new int[] {vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel,		vel};

		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	


	public void melody8(int note, int modulo,int subdivision,int arm, int eventIndex)
	{
	// num beats 44
	// start 60
	// arm 2

	int vel = 100;
	armArray = new int[] {        	arm,    arm,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm,    arm    };
	durArray = new int[] {        	50,    50,    50,    50,    100,        50,    50,    50,    50,    100,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    100,    50,    50,    50,    50,    100,    100,    50,    100,    50,    50,    50,    50,    50,    50,    50,    50,    50,    100,    150,    100,    50,    100,    50,    50,    150,    50,    100,    50,    50,    100,    50,    150,    50,    50,    50,    50,    100,    100,    100,    100    };
	pitchArray = new int[] {    	note+3,    	note+6,    	note+8,    	note+10,    note+9,    	note+3,    	note+6,    	note+8,    	note+13,    note+15,    note+3,    	note+6,    	note+8,    	note+9,    	note+15,    note+13,    note+18,    note+15,    note+20,    note+18,    note+15,    note+12,    note+10,    note+9,    	note+8,    	note+6,    	note+3,    	note+15,    note+13,    note+18,    note+13,    note+15,    note+13,    note+15,    note+13,    note+8,   	note+9,    	note+10,    note+13,    note+10,    note+9,    	note+8,    	note+6,    	note+3,    	note+1,    	note+3,    note+8,    note+9,    note+8,    note+6,    note+3,    note+9,    note+15,    note+18,    note+13,    note+15,    note+8,    note+13,    note+10,    note+6,    note+3,    note+6,    note+8,    note+9,    note+8,    note+6,    note+5       };
	velArray = new int[] {       	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    	vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel    };
	setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);

	}

//	public void melody9(int note, int modulo,int subdivision,int arm, int eventIndex)
//	{
//	// num beats 44
//	// start 60
//	// arm 2
//
//	int vel = 100;
//	armArray = new int[] {    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm,    arm+1,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm+1,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm    };
//	durArray = new int[] {    50,    50,    50,    50,    100,    50,    50,    50,    50,    100,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    50,    100,    50,    50,    50,    50,    100,    100,    50,    100,    50,    50,    50,    50,    50,    50,    50,    50,    50,    100,    150,    100,    50,    100,    50,    50,    150,    50,    100,    50,    50,    100,    50,    150,    50,    50,    50,    50,    100,    100,    100,    100        };
//	pitchArray = new int[] { note,    note+3,    note+5,    note+7,    note+6,    note+0,    note+3,    note+5,    note+10,    note+12,    note+0,    note+3,    note+5,    note+6,    note+12,    note+10,    note+15,    note+12,    note+17,    note+15,    note+12,    note+10,    note+7,    note+6,    note+5,    note+3,    note+0,    note+12,    note+10,    note+15,    note+10,    note+12,    note+10,    note+12,    note+10,    note+5,    note+6,    note+7,    note+10,    note+7,    note+6,    note+5,    note+3,    note+0,    note-2,    note+0,    note+5,    note+6,    note+5,    note+3,    note+0,    note+6,    note+12,    note+15,    note+10,    note+12,    note+5,    note+10,    note+7,    note+3,    note+0,    note+3,    note+5,    note+6,    note+5,    note+3,    note+2        };
//	velArray = new int[] {    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,            };
//	setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
//
//	}
	
	public void melody9(int note, int modulo,int subdivision,int arm, int eventIndex)
	{
	// num beats 44
	// start 60
	// arm 2

	int vel = 90;
	armArray = new int[] {    arm,    arm,    	arm,    	arm,    	arm,    	arm,    	arm,    	arm,    	arm+1,    	arm+1,   	arm,    	arm,    	arm,    	arm,    	arm+1,    	arm+1,    	arm+1,    	arm+1,    	arm+1,    	arm+1,    	arm+1,    	arm+1,    	arm,    	arm,    	arm+1,		arm,    	arm,    	arm,    	arm,    	arm,    	arm+1,    	arm,    	arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm,    arm,    arm+1,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm+1,    arm+1,    arm+1,    arm+1,    arm,    arm+1,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm,    arm    };
	durArray = new int[] {    50,    50,    	50,    		50,    		100,    	50,    		50,    		50,    		50,    		100,    	50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		50,    		0,			50,    		50,    		100,    	50,    		50,    		50,    		50,    		100,    	100,    50,    100,    50,    50,    50,    50,    50,    50,    50,    50,    50,    100,    150,    100,    50,    100,    50,    50,    150,    50,    100,    50,    50,    100,    50,    150,    50,    50,    50,    50,    100,    100,    100,    100        };
	pitchArray = new int[] { note,    note+3,   note+5,     note+7,    	note+6,    	note+0,    	note+3,    	note+5,    	note+10,    note+12,    note+0,    	note+3,    	note+5,    	note+6,    	note+12,    note+10,    note+15,    note+12,    note+17,    note+15,    note+12,    note+10,    note+7,    note+6,    	note+15,	note+5,    	note+3,    	note+0,    	note+12,    note+10,    note+15,    note+10,    note+12 };
	velArray=new int[pitchArray.length];
	for (int i=0;i<pitchArray.length;i++)
	{
		velArray[i]=70;
	}
	velArray[24]=0;
	
	setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}

	public void melody11(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		16 beats ... 72 = starting note
//		armArray=new int[] {	arm,		arm+1,			arm+1,			arm+0,		arm+1,			arm+1,			arm+1,			arm+1,			arm+0,		arm+1,			arm+1,			arm+1,		arm+0,		arm+0,		arm+1,		arm+1,			arm+1,			arm+1};
		durArray=new int[] {	66,			66,				66,				66,			66,				66,				133,			133,			133,		66,				66,				66,			66,			66,			66,			133,			133,			133};
		pitchArray=new int[] {	note+6,		note+11,		note+12,		note+6,		note+10,		note+11,		note+12,		note+11,		note+7,		note+12,		note+10,		note+9,		note+7,		note+5,		note+9,		note+10,		note+11,		note+12};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=90;
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}

	public void melody11_2(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		16 beats ... 72 = starting note
//		armArray=new int[] {	arm,		arm+1,			arm+1,			arm+0,		arm+1,			arm+1,			arm+1,			arm+1,			arm+0,		arm+1,			arm+1,			arm+1,		arm+0,		arm+0,		arm+1,		arm+1,			arm+1,			arm+1};
		durArray=new int[] {	66,			66,				66,				66,			66,				66,				133,			133,			133,		66,				66,				66,			66,			66,			66,			133,			133,			133};
		pitchArray=new int[] {	note+6,		note+11,		note+12,		note+6,		note+10,		note+11,		note+12,		note+11,		note+7,		note+12,		note+10,		note+9,		note+7,		note+5,		note+9,		note+10,		note+11,		note+12};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=90;
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
		armArray[2]=arm+1;
		armArray[6]=arm+1;
		armArray[9]=arm+1;
		armArray[pitchArray.length-1]=arm+1;
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	public void melody12(int note, int modulo,int subdivision,int arm, int eventIndex)
	{
		// System.out.prinln("melody 12");
	// num beats 16
	// start 81
	// arm 2

	int vel = 100;
	armArray = new int[] {        arm,    arm+1,    arm+1,    arm,    arm+1,    arm+1,    arm+1,    arm,    arm+1,    arm,    arm+1,    arm+1    };
	durArray = new int[] {        150,    150,    200,    100,    100,    100,    150,    150,    200,    100,    100,    100    };
	pitchArray = new int[] {    note,    note+4,    note+6,    note,    note+10,    note+9,    note+8,    note,    note+7,    note,    note+6,    note+5        };
	velArray = new int[] {        vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    vel,    };
	setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);

	}

	
	public void melody13(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		16 beats *highest = note+23  **arm+1 starts at note+16
		durArray=new int[] {50,50,50,50,100,100,150,150,100,150,150,100,150,150,100,50,50,50,50,100,100,150,150,100,150,150,100,150};
		pitchArray=new int[] {note,note+4,note+8,note+11,note+12,note+16,note+20,note+23,note+22,note+19,note+15,note+11,note+10,note+7,note+3,note,note+4,note+8,note+11,note+12,note+16,note+20,note+23,note+22,note+19,note+15,note+11,note+11};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=75;
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm+1;
		}
		armArray[0]=arm;
		armArray[1]=arm;
		armArray[14]=arm;
		armArray[15]=arm;
		armArray[16]=arm;
		
		armArray[pitchArray.length-1]=arm;

		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody14(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		7 beats *highest note = note+3;
		durArray=new int[] {150,100,100,150,100,100};
		pitchArray=new int[] {note,note+3,note+3,note+2,note+1,note+1};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			if (note<60)
			{
				velArray[i]=127;
			}
			else
			{
				velArray[i]=80;
			}
		}
		
		
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody15(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		9 beats  *highest note = note+12
		armArray=new int[] {arm,arm+1,arm+1,arm,arm,arm,arm,arm,arm+1,arm+1,arm+1,arm+1,arm+1};
		durArray=new int[] {150,150,200,100,100,100,150,150,200,100,100,100};
		pitchArray=new int[] {note,note+10,note+12,note,note+5,note+6,note+7,note+10,note+9,note+8,note+7,note+6};
		int vel;
		if (note<60)
		{
			vel=127;
		}
		else
		{
			vel=60;
		}
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
		velArray=new int[] {vel,vel,vel,vel,vel,vel,vel,vel,vel,vel,vel,vel};



		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	public void melody16(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		16 beats
//		base note = 48
		

		pitchArray=new int[] {note+10,		note+8,		note+7,		note+5,		note+4,		note+7,		note+1,		note+0,		note-2,		note+0,		note+1,		note-2,		note+0,		note+1,		note+4,		note+7,		note+10,		note+8,		note+7,		note+5,		note+4,		note+7,		note+1,		note+0,		note-2,		note+0,		note+1,		note-2,		note+0,		note+1,		note+0};
		velArray=new int[pitchArray.length];
		durArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			durArray[i]=50;
		}
		
		for (int i=0;i<pitchArray.length;i++)
		{
			if (note<72)
			{
				velArray[i]=127;
			}
			else
			{
				velArray[i]=70;
			}
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
//		for (int i=0;i<pitchArray.length;i++)
//		{
//			armArray[i]=arm+1;
//		}

		
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melody16_2(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		16 beats
//		base note = 48
		

		pitchArray=new int[] {note+10,		note+8,		note+7,		note+5,		note+4,		note+7,		note+1,		note+0,		note-2,		note+0,		note+1,		note-2,		note+0,		note+1,		note+4,		note+7,		note+10,		note+8,		note+7,		note+5,		note+4,		note+7,		note+1,		note+0,		note-2,		note+0,		note+1,		note-2,		note+0,		note+1,		note+0};
		velArray=new int[pitchArray.length];
		durArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			durArray[i]=50;
		}
		
		for (int i=0;i<pitchArray.length;i++)
		{
			if (note<72)
			{
				velArray[i]=100;
			}
			else
			{
				velArray[i]=70;
			}
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
			armArray[0]=arm+1;
			armArray[16]=arm+1;
		
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}

	public void melodyX(int note,int modulo,int subdivision,int arm,int eventIndex)
	{
//		9 beats
		durArray=new int[] {};
		pitchArray=new int[] {note};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=100;
		}
		armArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			armArray[i]=arm;
		}
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void melodyY(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		20 beats
		armArray=new int[] {arm};
		durArray=new int[] {};
		pitchArray=new int[] {note};
		velArray=new int[pitchArray.length];
		for (int i=0;i<pitchArray.length;i++)
		{
			velArray[i]=100;
		}
		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	public void melodyZ(int note,int modulo,int subdivision,int arm, int eventIndex)
	{
//		20 beats
		int vel = 100;
		armArray=new int[] {arm};
		durArray=new int[] {};
		pitchArray=new int[] {note};
		velArray=new int[] {vel};

		setValues2(armArray,subdivision,modulo,pitchArray,velArray,durArray, eventIndex);
	}
	
	public void follow1()
	{
		while(true)
		{
			double wait = .3;
			if (Main.notesArrayPointer>0){
				if (Main.notesArray[Main.notesArrayPointer-1][0]<92){

					noteOut(Main.notesArray[Main.notesArrayPointer-1][0], 120,1);
					wait(wait);
					noteOut(Main.notesArray[Main.notesArrayPointer-1][0]-5, 120,0);
					wait(wait);
					noteOut(Main.notesArray[Main.notesArrayPointer-1][0]-2, 120,1);
					wait(wait);
					noteOut(Main.notesArray[Main.notesArrayPointer-1][0]-7, 120,0);

				}
			}
			wait(wait);
//			System.out.println("test");
		}
	}
	
	public void headbang(boolean on)
	{
		if (on)
		{
			oscArray = new Object[] {"BANG",1};
			OSC.main(oscArray,"/head-commands");
		}
		else 
		{
			oscArray = new Object[] {"BANG",0};
			OSC.main(oscArray,"/head-commands");	
		}
	}
	
	public void listenPose()
	{
		oscArray = new Object[] {"/listenpose",(int)(3*Math.random())};
		OSC.main(oscArray,"/head-commands");
	}
	
	public void waitResponse()
	{
		Main.wait=true;
		while(Main.wait)
		{
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public int nextIndex()
	{
		int i=0;
		while (noteScheduler[0][i]==1&&i<noteScheduler[0].length-1)
		{
			i=i+1;
		}
		return i;
	}
	public void scheduleNote(int index, int[] values)
	{
		for (int i=0;i<values.length;i++)
		{
			noteScheduler[i][index]=values[i];
		}
	}
	public int nextEvent()
	{
		int i=0;
		while (scheduler[0][i]==1&&i<3)
		{
			i=i+1;
		}
		return i;
	}
		
	public void setValues(int[] _arm,int subdivision,  int modulo, int[] _notes, int[] _vel, int[] _dur, int eventIndex)
	{
		int i=eventIndex;
		for (int index=0;index<2050;index++)
		{
			scheduler[index][i]=0;
		}
		// System.out.prinln("setValues");
		scheduler[0][i]=1; // on off
		scheduler[1][i]=1; // identifier
		scheduler[2][i]=modulo; // modulo
		scheduler[3][i]=(count+1)%modulo; // offset
		scheduler[4][i]=40;
		scheduler[5][i]=0; // no longer in use 
		scheduler[6][i]=subdivision; // subdivision of modulo quarters
		
		for (int j=0;j<_notes.length;j++)
		{
			scheduler[j+50][i]=_notes[j];
		}
		for (int j=0;j<_vel.length;j++)
		{
			scheduler[j+550][i]=_vel[j];
		}
		for (int j=0;j<_dur.length;j++)
		{
			scheduler[j+1050][i]=_dur[j];
		}
		for (int j=0;j<_arm.length;j++)
		{
			scheduler[j+1550][i]=_dur[j];
		}
	}
	
	
	public void setValues2(int[] _arm,int subdivision,  int modulo, int[] _notes, int[] _vel, int[] _dur,int eventIndex)
	{
		// System.out.prinln(scheduler[0][0]+" ******  "+scheduler[0][1]+" ****** "+scheduler[0][2]+" ******* "+scheduler[0][3]);
		int i=eventIndex;
		for (int index=0;index<250;index++)
		{
			scheduler[index][i]=0;
		}
		// System.out.prinln("setValues2");
		scheduler[0][i]=1; // on off
		scheduler[1][i]=1; // identifier
		scheduler[2][i]=modulo; // modulo
		scheduler[3][i]=(count+1)%modulo; // offset
		scheduler[4][i]=40;
		scheduler[5][i]=0; //no longer in use
		scheduler[6][i]=subdivision; // subdivision of modulo quarters
		
		for (int j=0;j<_notes.length;j++)
		{
			scheduler[j+50][i]=_notes[j];
		}
		for (int j=0;j<_vel.length;j++)
		{
			scheduler[j+550][i]=_vel[j];
		}
		for (int j=0;j<_dur.length;j++)
		{
			scheduler[j+1050][i]=_dur[j];
		}
		for (int j=0;j<_arm.length;j++)
		{
			scheduler[j+1550][i]=_arm[j];
		}
		
		
	}
	
	

	public void setValuesNote(int column,int _arm,int subdivision, int modulo, int beat)
	{
		for(int i=0;i<400;i++)
		{
			int thisOnset = 0;
			for (int j=0;j<i;j++)
			{
				thisOnset=(int) (thisOnset+scheduler[j+1050][column]*.01*Main.quarter*modulo/subdivision);
			}
//			System.out.println("Main.quarter"+Main.quarter);
//			System.out.println("modulo = "+modulo);
//			System.out.println("thisOnset = "+thisOnset);
//			System.out.println("Main.quarter = "+Main.quarter);
			thisOnset=thisOnset%(modulo*Main.quarter);
			if (beat==0&&endrepeat)
			{
	        	Main.score.clearScheduler(1);
	        	Main.score.clearScheduler(0);
	        	Main.score.clearScheduler(2);

			}
			else
			{
				int thisBeat = beat*Main.quarter;
				int nextBeat = (beat+1)*Main.quarter;
				
	//			System.out.println("thisBeat "+thisBeat);
	//			System.out.println("nextBeat "+nextBeat);
	//			System.out.println("thisOnset "+thisOnset);
				if (thisOnset>=thisBeat&&thisOnset<nextBeat&&scheduler[i+50][column]>0)
				{			
					pitchArray = new int[] {1,thisOnset-thisBeat,scheduler[i+50][column],scheduler[i+550][column],scheduler[i+1550][column]};
					scheduleNote(nextIndex(),pitchArray);
	//				System.out.println("scheduled "+i);
				}
			}
		}
	}
	public void scheduleEvents(int _count)
	{
//		System.out.println("new beat");
//		if (_count%4==0){
//			oscArray = new Object[] {"BEAT",(int) (Main.quarter*3)};
//			OSC.main(oscArray,"/head-commands");
//		}
		count=_count;
		for (int i=0;i<4;i++)
		{


			if (scheduler[0][i]==1)
				
			{
				switch(scheduler[1][i])
				{
				case(1):
					setValuesNote(i,scheduler[5][i],scheduler[6][i], scheduler[2][i], (_count-scheduler[3][i])%scheduler[2][i]);
					break;
				}
			}
		}
	}
	public void clearNoteScheduler()
	{
//
//		scheduler = new int[scheduler.length][scheduler[0].length]; 
//		noteScheduler = new int[noteScheduler.length][noteScheduler[0].length]; 
		
//		for(int i=0;i<250;i++)
//		{
//			for(int j=0;j<4;j++)
//				scheduler[i][j]=0;
//		}
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<500;j++)
				noteScheduler[i][j]=0;
		}
	}
	public void clearScheduler(int index)
	{
//
//		scheduler = new int[scheduler.length][scheduler[0].length]; 
//		noteScheduler = new int[noteScheduler.length][noteScheduler[0].length]; 
		
//		for(int i=0;i<250;i++)
//		{
//			for(int j=0;j<4;j++)
//				scheduler[i][j]=0;
//		}
			for(int j=0;j<2050;j++)
			{
				scheduler[j][index]=0;
			}
			for(int j=0;j<500;j++)
			{
				noteScheduler[index][j]=0;
			}
		
			
	}
	public void clearScheduler()
	{
//
//		scheduler = new int[scheduler.length][scheduler[0].length]; 
//		noteScheduler = new int[noteScheduler.length][noteScheduler[0].length]; 
		
		for(int i=0;i<2050;i++)
		{
			for(int j=0;j<4;j++)
				scheduler[i][j]=0;
		}
		for(int i=0;i<5;i++)
		{
			for(int j=0;j<500;j++)
				noteScheduler[i][j]=0;
		}
	}
	
	public void metro()
	{
		noteScheduler = new int[noteScheduler.length][noteScheduler[0].length]; 
		Metro metro = new Metro();
		Thread metroThread = new Thread(metro);
//		metroThread.setPriority(Thread.MAX_PRIORITY);
		metroThread.start();
	}

	public void waitMil(double wait) 
	{
		long startTime=System.nanoTime();
		long triggerTime=startTime+(long)(1000000*wait*Main.quarter);
		long currentTime=System.nanoTime();
		
		while (currentTime<triggerTime)
		{
			triggerTime=startTime+(long)(1000000*wait);
			currentTime=System.nanoTime();
		}
	}
	
	public void wait(double wait) 
	{
		long startTime=System.nanoTime();
		long triggerTime=startTime+(long)(1000000*wait*Main.quarter);
		long currentTime=System.nanoTime();
		
		while (currentTime<triggerTime)
		{
			triggerTime=startTime+(long)(1000000*wait*Main.quarter);
			currentTime=System.nanoTime();
		}
	}
	public void noteOut(int note, double velocity,int arm){
		// System.out.prinln("-------------------TEST"+noteArray[1]);
		int vel = (int) (velocity*Main.velocity/100.);
		noteArray[0] = arm;
		noteArray[1] = note;
		noteArray[2] = vel;
		
		OSC.main(noteArray,"/directControl");
		int[] intNoteArray=new int[3];
		intNoteArray[0]=(Integer) noteArray[0];
		intNoteArray[1]=(Integer) noteArray[1];
		// System.out.prinln("note out: "+noteArray[1]);
	}
	public void noteOut2(int note, double velocity,double wait){
		int vel = (int) (velocity*Main.velocity/100.);
//		noteArray[0] = arm;
		noteArray[0] = note;
		noteArray[1] = vel;
		
		OSC.main(noteArray,"/mididata");
		int[] intNoteArray=new int[3];
		intNoteArray[0]=(Integer) noteArray[0];
		intNoteArray[1]=(Integer) noteArray[1];
		// System.out.prinln("note out: "+noteArray[1]);
		wait(wait);
	}
	public static void improvise()
	{
		Main.improvise.playing=false;
		Main.improvise.pointer=0;
		Main.improvise.stop=false;
		try 
		{
			Main.improvise.start();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void stopImprovise()
	{
		Main.improvise.playing=true;
		Main.improvise.stop=true;
	}
	


}



